﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FlowCard
{
    public partial class frmLogViewer : Form
    {
        string strGDNo;
        string strOperator;

        public frmLogViewer(string strTmpOperator,string strTmpGDNo)
        {
            strGDNo =strTmpGDNo ;
            strOperator = strTmpOperator;
            InitializeComponent();
        }

        private void frmLogViewer_Load(object sender, EventArgs e)
        {
            Query();
        }

        private void Query()
        {
            SQLTools sqt = new SQLTools();
            string strSelectString = "select ID 序号,GDNo 工单号,Operator 打印人,PrintTime 打印时间,Data 数据,Failure 已失效,Editor 处理人,EditTime 处理时间 from CardPrintLog where GDNo='@GDNo'";
            dgv1.DataSource = sqt.GetDataTable(strSelectString.Replace("@GDNo", strGDNo));
            dgv1.AutoResizeColumns();
            dgv1.Columns["数据"].Visible = false;
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                txtOperator.Text =GetString ( dgv1.CurrentRow.Cells["打印人"].Value);
                txtPrintTime.Text = GetString(dgv1.CurrentRow.Cells["打印时间"].Value);
                txtEditor.Text = GetString(dgv1.CurrentRow.Cells["处理人"].Value);
                txtEditTime.Text = GetString(dgv1.CurrentRow.Cells["处理时间"].Value);
                string[] strTmp = GetString(dgv1.CurrentRow.Cells["数据"].Value).Split('|');
                StringBuilder tmpsb = new StringBuilder();
                for (int i = 0; i < strTmp.Length; i++)
                {
                    tmpsb.AppendLine(strTmp[i]);
                }
                txtData.Text = tmpsb.ToString();
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void 设置为失效ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                LogHelper.MakeLogFailure(strOperator, GetString(dgv1.CurrentRow.Cells["序号"].Value));
                dgv1.CurrentRow.Cells["已失效"].Value = true;
                Query();
            }
        }
    }
}
